package cn.fx.user.service.impl;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.service.impl.BaseServiceImpl;
import cn.fx.basic.util.Md5Utils;
import cn.fx.basic.util.StrUtils;
import cn.fx.user.domain.Logininfo;
import cn.fx.user.domain.User;
import cn.fx.user.dto.RegisterDto;
import cn.fx.user.mapper.LogininfoMapper;
import cn.fx.user.mapper.UserMapper;
import cn.fx.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {

        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();

        //校验 非空校验
        if(StringUtils.isEmpty(phone)
            || StringUtils.isEmpty(verifyCode)
            || StringUtils.isEmpty(password)
            || StringUtils.isEmpty(comfirmPassword)
        ){
            throw new BusinessException("参数不能为空！");
        }

        //两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinessException("两次密码不一致，请重新输入!");
        }

        //手机号校验 是否被注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone != null){
            throw new BusinessException("手机号已经注册,请直接登录！");
        }

        //验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + "phoneRegister");
        if(obj == null){
            //验证码过期
            throw new BusinessException("验证码已过期,请重新获取！");
        }

        //校验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw new BusinessException("验证码输入错误,请重新输入");
        }

        //获取logininfo对象和user对象
        Logininfo logininfo = dto2logininfo(dto);
        //通过logininfo对象获取user对象
        User user = logininfo2User(logininfo);

        //先保存logininfo表，返回自增的主键
        logininfoMapper.save(logininfo);

        //把自增的主键赋值给user对象,再保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User logininfo2User(Logininfo logininfo) {

        User user = new User();
        // copy相同属性
        BeanUtils.copyProperties(logininfo,user);

        return user;
    }

    /**
     * 创建logininfo对象
     * @param dto
     * @return
     */
    public Logininfo dto2logininfo(RegisterDto dto){

        Logininfo logininfo = new Logininfo();

        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);

        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);

        //通过盐值和密码进行加密  加盐加密
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);

        return logininfo;
    }
}
